package com.level2app.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;

import androidx.multidex.MultiDex;

import com.level2app.di.DaggerAppComponent;
import com.level2app.di.AppComponent;
import com.level2app.di.AppModule;
import com.level2app.di.retrofit.RetrofitModule;
import com.level2app.utils.ActivityStackManager;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

public class MyApplication extends Application {
    private static MyApplication instance;
    private static Context context;
    private AppComponent mAppComponent;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        instance = this;
        initComponent();
        MultiDex.install(this);
    }

    public static Context getContext() {
        return context;
    }

    public static MyApplication getInstance() {
        return instance;
    }

    private void initComponent() {
        this.mAppComponent = DaggerAppComponent
                .builder()
                .retrofitModule(new RetrofitModule())
                .appModule(new AppModule(this))
                .build();
    }

    public AppComponent getAppComponent() {
        return this.mAppComponent;
    }

    static {
        //设置全局的Header和Footer构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> new ClassicsHeader(context));
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> new ClassicsFooter(context));
    }

    //添加Activity到容器中
    public void addActivity(Activity activity) {
        ActivityStackManager.getInstance().pushActivity(activity);
    }

    //遍历所有Activity并finish
    public void exit() {
        ActivityStackManager.getInstance().exitActivity();
    }

    //弹出除一个以外的所有活动
    public void popAllActivityExceptOne() {
        ActivityStackManager.getInstance().popAllActivityExceptMain();
    }

    //退出app
    public void exitApp() {
        ActivityStackManager.getInstance().exitActivity();
        System.exit(0);
    }
}
